package com.go.where.family.component.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.go.where.family.component.CustomerAddressComponent;
import com.go.where.family.constants.TransmissionCode;
import com.go.where.family.exception.ServerDisponseException;
import com.go.where.family.pojo.CustomerAddressInfo;
import com.go.where.family.scoket.Head;
import com.go.where.family.service.CustomerAddressService;
import com.go.where.family.utils.CommonUtil;
import com.go.where.family.vo.CustomerAddressVo;
import com.google.gson.Gson;

@Component("customerAddressComponent")
public class CustomerAddressComponentImpl implements CustomerAddressComponent {

	private static final Logger log = Logger.getLogger(CustomerAddressComponentImpl.class);
	private Gson gson;
	@Resource(name = "customerAddressService")
	private CustomerAddressService customerAddressService;

	public CustomerAddressComponentImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public List<CustomerAddressInfo> queryList(String body, Head head) {
		CustomerAddressVo vo = gson.fromJson(body, CustomerAddressVo.class);
		if (vo != null) {
			Integer currentPage = vo.getPageIndex();
			Integer pageSize = vo.getPageCount();
			Integer pageStart = 1;
			Integer pageEnd = 10;
			if (currentPage != null && pageSize != null) {
				pageStart = (currentPage - 1) * pageSize + 1;
				pageEnd = currentPage * pageSize;
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("pageStart", pageStart);
			map.put("pageEnd", pageEnd);
			map.put("customerId", vo.getCustomerId());

			List<CustomerAddressInfo> list = customerAddressService.queryList(map);
			if (list != null && !list.isEmpty()) {
				return list;
			} else {
				return new ArrayList<CustomerAddressInfo>();
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",获取客户地址列表信息时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST.getCode(),
					TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public CustomerAddressInfo queryAddressDetail(String body, Head head) {
		CustomerAddressVo vo = gson.fromJson(body, CustomerAddressVo.class);
		if (vo != null) {
			String id = vo.getId();
			CustomerAddressInfo address = customerAddressService.queryByPrimartKey(id);
			if (address != null) {
				return address;
			} else {
				log.info("requestId:" + head.getRequestId() + ",查询地址详情信息时,没有查询到数据,视频ID：" + id);
				throw new ServerDisponseException(TransmissionCode.NO_DATA.getCode(),
						TransmissionCode.NO_DATA.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",获取地址详情信息时，报文解析异常");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST.getCode(),
					TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public boolean save(String body, Head head) {
		CustomerAddressVo vo = gson.fromJson(body, CustomerAddressVo.class);
		if (vo != null) {
			CustomerAddressInfo address = new CustomerAddressInfo();
			address.setAddress(vo.getAddress());
			address.setConsignee(vo.getConsignee());
			address.setCustomerId(vo.getCustomerId());
			address.setMobile(vo.getMobile());
			address.setPostcode(vo.getPostcode());
			address.setRegion(vo.getRegion());
			address.setId(UUID.randomUUID().toString());
			int re = customerAddressService.save(address);
			if (re > 0) {
				return true;
			} else {
				return false;
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",保存客户地址信息时，报文解析异常");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST.getCode(),
					TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

}
